package com.tridoron.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tridoron.reggie.common.CustomException;
import com.tridoron.reggie.dto.SetmealDto;
import com.tridoron.reggie.entity.Dish;
import com.tridoron.reggie.entity.DishFlavor;
import com.tridoron.reggie.entity.Setmeal;
import com.tridoron.reggie.entity.SetmealDish;
import com.tridoron.reggie.mapper.SetmealMapper;
import com.tridoron.reggie.service.DishService;
import com.tridoron.reggie.service.SetmealDishService;
import com.tridoron.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 添加套餐
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存基本信息
        this.save(setmealDto);

        List<SetmealDish> setmealDishList=setmealDto.getSetmealDishes();
        setmealDishList.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息
        setmealDishService.saveBatch(setmealDishList);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void remove(Long[] ids) {
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        this.removeByIds(Arrays.asList(ids));

        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);
    }

    /**
     * 修改status属性
     * @param status
     * @param ids
     */
    @Override
    public void updateStatusById(Integer status, Long[] ids) {

        for (Long id : ids) {
            boolean flag=true;
            Setmeal setmeal = this.getById(id);
            //如果需要启售套餐，查看套餐下是否有停售菜品
            if (status==1){
                Long setmealId=setmeal.getId();
                LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
                setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
                List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
                List<Long> dishIdList = list.stream().map(SetmealDish::getDishId).collect(Collectors.toList());
                for (Long dishId : dishIdList) {
                    if(dishService.getById(dishId).getStatus()!=1){
                        flag=false;//存在停售菜品
                    }
                }
                if (!flag){
                    throw new CustomException("当前套餐下存在停售菜品，不能启售");
                }
            }
            if (setmeal.getStatus()!=status) {
                setmeal.setStatus(status);
            }
            this.updateById(setmeal);
        }
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        Setmeal setmeal=this.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes=setmealDishService.list(setmealDishLambdaQueryWrapper);

        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    /**
     * 更新数据
     * @param setmealDto
     */
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);

        List<SetmealDish> setmealDishes=setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

}
